/*
 * AudioScreen.java
 *
 * Created on March 16, 2006, 5:37 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package Media;

import Comm.Util.Logger;
import Experience.*;
import Experience.Screens.ChoiceScreen;
import javax.microedition.lcdui.*;

import javax.microedition.midlet.MIDlet;

import java.io.IOException;
import java.util.Hashtable;

/**
 *
 * @author dmgreen
 */
public class AudioScreen extends ChoiceScreen {
    
    private MIDlet myMIDlet = null;
    
    private AudioMgr myAudio = null;
    
    private final String P_STOPPED   = "Stopped";
    private final String R_STOPPED   = "Stopped";
    private final String READY     = "Ready"; // before you've recorded anything.
    private final String RECORDING = "Recording";
    private final String PLAYING   = "Playing";
    private Image recImage,recStopImage,playImage,playStopImage ;
    
    private Image stoppedImage, playingImage, readyImage, recordingImage;
    
    private Hashtable statusMap = new Hashtable();
    
    private String MODE = READY;
    
    private int status_y;
    private int status_x; 
    
    private int status_icon_x;
    private int status_icon_y;
    
    private int icon_x;
    private int icon_y;
    private int icon_anchor;
    
    /** Creates a new instance of AudioScreen */
    public AudioScreen(MIDlet expsamp) {
        
        super(expsamp, "Press trigger button to start recording audio", 0,  "", "Next");
        myMIDlet = expsamp;
        
        myAudio = new AudioMgr(this);
        
        status_x = (getWidth()  / 2) - 75; //because the icons are 150 wide
        status_y = getHeight() / 3;
        
        icon_x = getWidth()/2-13;
        icon_y = getHeight()-5;
        icon_anchor = Graphics.BOTTOM | Graphics.LEFT;
    
        try {
            recImage = Image.createImage("/recording-start_ico.png");
            recStopImage = Image.createImage("/recording-stop_ico.png");
            playImage = Image.createImage("/playback-start_ico.png");
            playStopImage = Image.createImage("/playback-stop_ico.png");
            
            stoppedImage = Image.createImage("/stopped_ico.png"); 
            playingImage = Image.createImage("/playing_ico.png");
            readyImage   = Image.createImage("/ready_ico.png");
            recordingImage = Image.createImage("/recording_ico.png");
            
        }
        catch (IOException ioe) {
            Logger.getLog().addLogMessage("Graphic NOT FOUND", Logger.MSG_ERROR);
        }
        
        statusMap.put(P_STOPPED, stoppedImage);
        statusMap.put(R_STOPPED, stoppedImage);
        statusMap.put(READY, readyImage);
        statusMap.put(RECORDING, recordingImage);
        statusMap.put(PLAYING, playingImage);
    
    }
    
    public void paint(Graphics g) {
        int color = g.getColor();
        g.setColor(255,255,255);
        g.fillRect(0,0,getWidth(), getHeight());
        g.setColor(color);
        
        
        if (MODE == READY) {
            setText("Press trigger button to start recording audio");
            setLeftLabel("");
            setRightLabel("Next");
             super.paint(g);
            g.drawImage(recImage, icon_x, icon_y, icon_anchor);
        }
        else if (MODE == RECORDING) {
            setText("Press trigger button to stop recording.");
            setRightLabel("");
            setLeftLabel("");
             super.paint(g);
            g.drawImage(recStopImage, icon_x, icon_y, icon_anchor);
        }
        else if (MODE == P_STOPPED || MODE == R_STOPPED) {
            setText("Press trigger button down to play recording / " +
                    "right to create new recording.");
            setLeftLabel("Delete");
            setRightLabel("Next");
             super.paint(g);
            g.drawImage(playImage, icon_x, icon_y, icon_anchor);
        }
        
        else if (MODE == PLAYING) {
            setText("Use trigger button to stop playback.");
            setLeftLabel("");
            setRightLabel("");
            super.paint(g);
            g.drawImage(playStopImage, icon_x, icon_y, icon_anchor);
        }
        
        //Font curF = g.getFont();
        //g.setFont(largeFont);
        //g.drawString(MODE, status_x, status_y, Graphics.TOP | Graphics.LEFT);
        //g.setFont(curF);
        
        Image status_image = (Image) statusMap.get(MODE);
        g.drawImage(status_image, status_x, status_y, Graphics.TOP | Graphics.LEFT);
    }
    
    protected void keyReleased(int keyCode) {
        
        int action = getGameAction(keyCode);
        super.keyReleased(keyCode);
        
               
        if (action == 8) {
           
            if (MODE == READY) {
              myAudio.startRecording();
              MODE   = RECORDING;
              //setRightLabel("");
              //setLeftLabel("");
            }
           
           else if (MODE == RECORDING) {
               byte[] audio_data = myAudio.stopRecording();
               MODE = R_STOPPED;
               //setLeftLabel("Delete");
               //setRightLabel("Next");
               ((ExpUI) myMIDlet).addAudioToSample(audio_data);
           }
            
           else if (MODE == R_STOPPED || MODE == P_STOPPED) {
                myAudio.playRecording();
                MODE = PLAYING;
                //setLeftLabel("");
                //setRightLabel("");
           }
            
            else if (MODE == PLAYING) {
                doStopPlaying();
            } 
            repaint();   
        }
        
        else if (action == Canvas.RIGHT) {
            if (MODE == R_STOPPED || MODE == P_STOPPED) {
                MODE = READY;
                //setLeftLabel("");
                //setRightLabel("Next");
                repaint();
            }
        }
    }
    
    public void doStopPlaying() {
        //this needs to be encapsulated so it can be called from
        //the PlayerListener
        myAudio.stopPlaying();
        MODE = P_STOPPED;
        setLeftLabel("Delete");
        setRightLabel("Next");
        repaint();
    }
    
    protected void leftPressed() {
        if (MODE == R_STOPPED || MODE == P_STOPPED) {
            //left label is delete
            ((ExpUI) myMIDlet).deleteLastAudio();
            setLeftLabel("");
            MODE = READY;
            repaint();
        }
        
    }
    
    protected void rightPressed() {
        if ( ((ExpUI)myMIDlet).display.getCurrent() != this) {return;}
     
        if (MODE == R_STOPPED || MODE == P_STOPPED || MODE == READY) {
            ((ExpUI) myMIDlet).notifyNext("");
        }
    }
    
   
    public void cleanUp() {
        if (myAudio != null) {
            myAudio.close();
        }
        
        MODE = READY;
        repaint();
    } 
}


